package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.PropertyBuilding;
import com.lucy.property.entity.PropertyUnit;
import com.lucy.property.params.BuildingCreateParams;
import com.lucy.property.params.BuildingQueryParams;
import com.lucy.property.params.BuildingUpdateParams;
import com.lucy.property.service.PropertyBuildingService;
import com.lucy.property.service.PropertyUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@RestController
@Api(tags = "楼栋管理")
public class BuildingController {
    @Autowired
    PropertyBuildingService service;

    @Autowired
    PropertyUnitService unitService;

    @PostMapping("buildings")
    @ApiOperation(value = "楼栋新增")
    public ResponseDTO<Object> buildingCreate(@RequestBody @Validated BuildingCreateParams params){
        PropertyBuilding building = new PropertyBuilding();
        // 获取所在单元信息
        Optional<PropertyUnit> unitOp = unitService.getOne(params.getUnitId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"无对应单元信息");
        }
        PropertyUnit unit = unitOp.get();
        building.setUnitCode(unit.getUnitCode());
        building.setUnitName(unit.getUnitName());
        // 创建参数
        BeanUtil.copyProperties(params,building, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.saveOne(building)
        );
    }

    @DeleteMapping("buildings/{id}")
    @ApiOperation(value = "楼栋删除")
    public ResponseDTO<Object> buildingDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("buildings")
    @ApiOperation(value = "楼栋更新")
    public ResponseDTO<Object> buildingUpdate(@RequestBody @Validated BuildingUpdateParams params){
        Optional<PropertyBuilding> buildingOp = service.getOne(params.getId());
        if (!buildingOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }

        PropertyBuilding building = buildingOp.get();
        // 获取所在单元信息
        Optional<PropertyUnit> unitOp = unitService.getOne(params.getUnitId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"无对应单元信息");
        }
        PropertyUnit unit = unitOp.get();
        building.setUnitCode(unit.getUnitCode());
        building.setUnitName(unit.getUnitName());
        //
        BeanUtil.copyProperties(params,building,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.updateOne(building)
        );
    }

    @GetMapping("buildings")
    @ApiOperation(value = "楼栋列表")
    public ResponseDTO<Object> buildingListQuery(BuildingQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        PropertyBuilding building = new PropertyBuilding();
        BeanUtil.copyProperties(params,building,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<PropertyBuilding> example = Example.of(building);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("buildings/{id}")
    @ApiOperation(value = "楼栋查询",notes = "")
    public ResponseDTO<Object> buildingQuery(@PathVariable("id") String id){
        Optional<PropertyBuilding> buildingOp = service.getOne(id);
        return buildingOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }

    @GetMapping("buildings/all")
    @ApiOperation(value = "全部楼栋",notes = "")
    public ResponseDTO<Object> buildingAll(){
        PropertyBuilding building = new PropertyBuilding();
        Example<PropertyBuilding> example = Example.of(building);
        List<PropertyBuilding> res = service.getExample(example);

        return ResponseDTO.succData(
                res
        );
    }
}
